$(function() {
  var all_url = 'http://api20.scsj.net.cn'
  var userId = $.cookie('userId')
  var token = $.cookie('token')
  var items;

  // 获取appid等
  $.ajax({
      type: 'get',
      url: 'http://home.scsj.net.cn/wyb/h5/wx/sample.php',
      success: function(data) {
        data = JSON.parse(data);
        console.log(data);
        // 配置微信公众号验证权限
        wx.config({
          debug: true,
          appId: data.appId, // 企业号的唯一标识
          timestamp: data.timestamp, // 签名的时间戳
          nonceStr: data.nonceStr, // 生成签名的随机串
          signature: data.signature, // 签名
          jsApiList: [
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'onMenuShareQZone'
          ]
        })
      }
    })
    // 二维码请求
  $.ajax({
      type: 'post',
      url: all_url + '/user/requestqrcode.do',
      data: {
        userId: userId,
        tocken: token
      },
      success: function(data) {
        $('.qrCode img').attr('src', data.result.qrcode);
        var search = data.result.qrcode;
        var pattern = new RegExp('[?&]s\=([^&]+)', 'g');
        var matcher = pattern.exec(search);
        $('.qrCode img').attr('src', data.result.qrcode);
        if (null != matcher) {
          try {
            items = decodeURIComponent(decodeURIComponent(matcher[1]))
          } catch (e) {
            try {
              items = decodeURIComponent(matcher[1])
            } catch (e) {
              items = matcher[1]
            }
          }
        }
        console.log(items);
        // 配置分享信息并通过ready接口处理
        window.share_config = {
          'share': {
            'imgUrl': '/h5/favicon.ico',
            'desc': '欢迎注册商超财富宝',
            'title': '注册 | 商超财富宝',
            'link': items,
            'success': function () {},
            'cancel': function () {}
          }
        }
        wx.ready(function () {
          wx.onMenuShareAppMessage(share_config.share); // 分享给好友
          wx.onMenuShareTimeline(share_config.share); // 分享到朋友圈
          wx.onMenuShareQQ(share_config.share); // 分享给手机QQ
        })
      },
      error: function() {
        $.toast('二维码获取失败')
      }
    })
    // 获取用户推广码
  $.ajax({
    url: all_url + '/user/getUserDetail.do',
    type: 'post',
    dataType: 'json',
    data: {
      userId: userId,
      tocken: token
    },
    success: function(data) {
      $('#qrnum').html(data.result.code)
    },
    error: function() {
      $.toast('邀请码获取失败')
    }
  })

  $(".btn").click(function() {
    wx.onMenuShareAppMessage({
      title: '商超财富宝',
      desc: '商超世界欢迎您的加入',
      link: items,
      imgUrl: '../favicon.ico',
      trigger: function(res) {
        alert('用户点击发送给朋友');
      },
      success: function(res) {
        alert('已分享');
      },
      cancel: function(res) {
        alert('已取消');
      },
      fail: function(res) {
        alert(JSON.stringify(res));
      }
    });
    alert('已注册获取“发送给朋友”状态事件');
  })
})